package com.ouzexi.finalexp

import android.content.Intent
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.ouzexi.finalexp.util.save
import com.ouzexi.finalexp.vo.loginReturn
import kotlinx.android.synthetic.main.activity_login.*
import okhttp3.*
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.IOException

class login : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        // 点击注册跳转
        reg.setOnClickListener{
            val intent = Intent(this, register::class.java)
            startActivity(intent)
        }

        // 点击忘记密码跳转
        forgotten.setOnClickListener{
            val intent = Intent(this, find_password::class.java)
            startActivity(intent)
        }

    }

    fun login(view: View) {
        val that = this
        val user = username.text
        val passwd  = password.text
        val urlAPI = "http://10.0.2.2:8080/school/student/login"
        val json = JSONObject()
        json.put("username", user)
        json.put("password", passwd)
        val requestBody:RequestBody = json.toString().toRequestBody()

        val request = Request.Builder()
        request.url(urlAPI)
        request.addHeader("Content-Type","application/json")
            .post(requestBody)
            .url(urlAPI).build()

        val client = OkHttpClient()

        client.newCall(request.build()).enqueue(object : Callback {

            override fun onResponse(call: Call, response: Response) {
                val result = response.body?.string()
                Log.i("testReponse",result.toString())
                val dataObj:loginReturn = Gson().fromJson(result, loginReturn::class.java)
                if(dataObj.code==-1) {
                    Log.i("code",dataObj.code.toString())
                    Log.i("test","登录失败")
                    Looper.prepare()
                    Toast.makeText(that,"账号或密码错误",Toast.LENGTH_SHORT).show()
                    Looper.loop()
                }
                else {
                    Log.i("test","登录成功")
                    // 与数据库匹配
                    Looper.prepare()
                    val token = dataObj.token
                    Log.i("token",token)
                    save().saveData(that,"token",token)
                    Toast.makeText(that,"登录成功",Toast.LENGTH_SHORT).show()
                    val intent = Intent(that, mine::class.java)
                    startActivity(intent)
                    Looper.loop()
                }
            }

            override fun onFailure(call: Call, e: IOException) {
                println("Failed request api :( " + e.message)
                Log.i("testRequest",e.message.toString())
            }

        })
    }
}